package com.example.hasee.datepickerdialog;

import android.app.DatePickerDialog;
import android.icu.util.Calendar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.DatePicker;
import android.widget.Toast;

import java.util.Date;
import java.util.Locale;


public class DatePickerDialogExample extends AppCompatActivity {

    private TextView showdate;
    private Button setdate;
    private int year;
    private int month;
    private int day;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_picker_dialog_example);

        showdate=(TextView) this.findViewById(R.id.showtime);
        setdate=(Button) this.findViewById(R.id.setdate);
        //初始化Calendar日历对象
        java.util.Calendar c= java.util.Calendar.getInstance(Locale.CANADA);

        year=c.get(java.util.Calendar.YEAR);//获取Calendar对象中的年
        month=c.get(java.util.Calendar.MONTH);
        day=c.get(java.util.Calendar.DAY_OF_MONTH);
        showdate.setText("当前日期："+year+"-"+(month+1)+"-"+day);//显示当前年月日

        //添加单击事件——设置日期
        setdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建DatePickerDialog对象
                DatePickerDialog my_datePickerDialog= new DatePickerDialog(DatePickerDialogExample.this,Datelistener,year,month,day);
                my_datePickerDialog.show();//显示DatePickerDialog组件
            }
        });
    }
    private DatePickerDialog.OnDateSetListener Datelistener=new DatePickerDialog.OnDateSetListener()
    {
      @Override
      public void onDateSet(DatePicker v, int y, int m,int d) {
          Calendar time =  Calendar.getInstance(Locale.CHINA);
          time.set(y, m, d);

          if(time.getTimeInMillis()<= System.currentTimeMillis()) {
              //修改year、month、day的变量值，以便以后单击按钮时，DatePickerDialog上显示上一次修改后的值
              year = y;
              month = m;
              day = d;
              //更新日期
              updateDate();
          }
          else{
              Toast.makeText(DatePickerDialogExample.this,"输入无效",Toast.LENGTH_SHORT).show();
          }
      }
      //当DatePickerDialog关闭时，更新日期显示
        private void updateDate()
        {
            //在TextView上显示日期
            showdate.setText("当前日期："+year+"-"+(month+1)+"-"+day);
        }
    };
}
